﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Tickets
/// </summary>
public class Tickets
{
    DataClassesDataContext dtc;
	public Tickets()
	{
		//
		// TODO: Add constructor logic here
		//
        this.dtc = new DataClassesDataContext();
    }
    #region Cinema
    public List<tblCinema> LoadCinema()
    {
        var x = from i in this.dtc.tblCinemas
                orderby i.CinemaID ascending
                select i;
        return x.ToList();
    }
    public string GetCinemaID()
    {
        tblCinema d = (from t in this.dtc.tblCinemas
                        where t.Stt == this.dtc.tblCinemas.Max(t1 => t1.Stt)
                        select t).First();
        int id = d.Stt + 1;
        return "CN0" + id;

    }
    public bool AddCinema(string CNId, string CnName,int status)
    {
        try
        {
            tblCinema cn = new tblCinema();
            cn.CinemaID = CNId;
            cn.Name = CnName;
            cn.Status = status;
            this.dtc.tblCinemas.InsertOnSubmit(cn);
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    public bool UpdateCinema(string Cnid, string CnName, int status)
    {
        try
        {
            tblCinema cn = (from i in this.dtc.tblCinemas where i.CinemaID == Cnid select i).Single();
            cn.CinemaID = Cnid;
            cn.Name = CnName;
            cn.Status = status;
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    public tblCinema GetCinemaByName(string name)
    {
        tblCinema x = (from i in this.dtc.tblCinemas
                        where i.Name.Contains(name)
                        select i).First();
        return x;
    }
    public string GetCinemaNameByID(string id)
    {
        tblCinema x = (from i in this.dtc.tblCinemas
                       where i.CinemaID.Equals(id)
                       select i).First();
        return x.Name;
    }
    #endregion
    #region Chair
    public string GetChairID()
    {
        tblChair d = (from t in this.dtc.tblChairs
                       where t.Stt == this.dtc.tblChairs.Max(t1 => t1.Stt)
                       select t).First();
        int id = d.Stt + 1;
        return "CH0" + id;

    }
    public List<tblChair> LoadChair()
    {
        var x = from i in this.dtc.tblChairs
                orderby i.ChairID ascending
                select i;
        return x.ToList();
    }
    public List<tblChair> LoadChairByCinema(string Cid)
    {
        var x = from i in this.dtc.tblChairs
                where i.CinemaID.Equals(Cid)
                orderby i.ChairID ascending
                select i;
        return x.ToList();
    }
    public List<tblChair> LoadChairByCinemaAndID(string Cid,string idC)
    {
        var x = from i in this.dtc.tblChairs
                where i.CinemaID.Equals(Cid) && i.ChairID.Equals(idC)
                orderby i.ChairID ascending
                select i;
        return x.ToList();
    }
    public bool AddChair(string chid, string CinemaID, int Tier, int ChairNumber,int StatusC,string Notes)
    {
        try
        {
            tblChair ch = new tblChair();
            ch.ChairID = chid;
            ch.CinemaID = CinemaID;
            ch.Tier = Tier;
            ch.ChairNumber = ChairNumber;
            ch.StatusC = StatusC;
            ch.Notes = Notes;
            this.dtc.tblChairs.InsertOnSubmit(ch);
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    public bool UpdateChair(string chid,int StatusC, string Notes)
    {
        try
        {
            tblChair  ch = (from i in this.dtc.tblChairs where i.ChairID == chid select i).Single();
            ch.StatusC = StatusC;
            ch.Notes = Notes;
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    #endregion
    #region Ticket
    public string GetTicketID()
    {
        tblTicket d = (from t in this.dtc.tblTickets
                       where t.Stt == this.dtc.tblTickets.Max(t1 => t1.Stt)
                       select t).First();
        int id = d.Stt + 1;
        return "TK0" + id;

    }
    public List<tblTicket> LoadTicket()
    {
        var x = from i in this.dtc.tblTickets
                orderby i.TicketId ascending
                select i;
        return x.ToList();
    }
    public bool AddTicket(string tkid, string mvid, string chid, float price,DateTime datemv,DateTime timemv, int Statust, string Notes)
    {
        try
        {
            tblTicket tk = new tblTicket();
            tk.TicketId = tkid;
            tk.MovieID = mvid;
            tk.ChairID = chid;
            tk.Price = price;
            tk.DateMovie = datemv.Date;
            tk.TimeMovie = timemv.TimeOfDay;
            tk.StatusT = Statust;
            tk.Notes = Notes;
            this.dtc.tblTickets.InsertOnSubmit(tk);
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    public bool UpdateTicket(string tkid,string chid, float price, DateTime datemv, DateTime timemv, int Statust, string Notes)
    {
        try
        {
            tblTicket tk = (from i in this.dtc.tblTickets where i.TicketId == tkid select i).Single();
            tk.ChairID = chid;
            tk.Price = price;
            tk.DateMovie = datemv.Date;
            tk.TimeMovie = timemv.TimeOfDay;
            tk.StatusT = Statust;
            tk.Notes = Notes;
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    public List<tblTicket> LoadTicketByMovie(string mvid)
    {
        var x = from i in this.dtc.tblTickets
                where i.MovieID == mvid
                orderby i.TicketId ascending
                select i;
        return x.ToList();
    }
    public List<tblTicket> LoadTicketByDate(string mvid,DateTime date)
    {
        var x = from i in this.dtc.tblTickets
                where i.MovieID == mvid && i.DateMovie == date.Date
                orderby i.TicketId ascending
                select i;
        return x.ToList();
    }
    public List<tblTicket> LoadTicketByDateAndTime(string mvid, DateTime date,DateTime time)
    {
        var x = from i in this.dtc.tblTickets
                where i.MovieID == mvid && i.DateMovie == date.Date && i.TimeMovie == time.TimeOfDay
                orderby i.TicketId ascending
                select i;
        return x.ToList();
    }
    #endregion
}